package com.mingtu.dzpatrol.activity;

import android.view.View;
import android.widget.Button;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.blankj.utilcode.util.GsonUtils;
import com.blankj.utilcode.util.LogUtils;
import com.blankj.utilcode.util.SPStaticUtils;
import com.blankj.utilcode.util.StringUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.model.Response;
import com.mingtu.dzpatrol.R;
import com.mingtu.dzpatrol.base.BaseActivity;
import com.mingtu.dzpatrol.callback.StringDialogCallback;
import com.mingtu.dzpatrol.utils.ActivityUtil;
import com.mingtu.dzpatrol.utils.Constant;
import com.mingtu.dzpatrol.utils.IntentUtils;
import com.mingtu.dzpatrol.utils.MyUtills;
import com.mingtu.dzpatrol.utils.SPTools;
import com.mingtu.dzpatrol.view.ContainsEmojiEditText;


import java.util.HashMap;
import java.util.Timer;
import java.util.TimerTask;

import butterknife.BindView;
import butterknife.OnClick;

public class ResetPasswordActivity extends BaseActivity {

    @BindView(R.id.edit_old_password)
    ContainsEmojiEditText editOldPassword;
    @BindView(R.id.edit_new_password)
    ContainsEmojiEditText editNewPassword;
    @BindView(R.id.edit_sure_new_password)
    ContainsEmojiEditText editSureNewPassword;
    @BindView(R.id.but_save)
    Button butSave;
    private String oldPassword;
    private String newPassword;
    private String sureNewPassword;
    private Timer timer;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_reset_password;
    }

    @Override
    protected void initView() {
        setModuleTitle("修改密码");
    }

    @Override
    protected void initData() {

    }

    @Override
    protected void getData() {

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (timer != null) {
            timer.cancel();
        }
    }

    /**
     * 重置密码
     */
    private void resetPassword() {

        HashMap<String, Object> hashMap = new HashMap<>();
        hashMap.put("oldPassword", oldPassword);
        hashMap.put("newPassword", newPassword);
        String str = GsonUtils.toJson(hashMap);

        OkGo.<String>post(Constant.getInstance().POST_UPDATE_PASSWORD)
                .tag(this)
                .upJson(str)
                .execute(new StringDialogCallback(this) {
                    @Override
                    public void onSuccess(Response<String> response) {
                        String result = response.body();
                        if (!StringUtils.isEmpty(result)) {
                            JSONObject jsonObject = JSON.parseObject(result);
                            String msg = jsonObject.getString("msg");
                            if (msg.equals(Constant.RESULT_SUCCESS)) {
                                ToastUtils.showLong("密码修改成功！");
                                //实例化Timer类 ,守护线程，不在后台一直运行
                                timer = new Timer(true);
                                timer.schedule(new TimerTask() {
                                    public void run() {
                                        SPStaticUtils.remove(SPTools.token);
                                        IntentUtils.getInstance().readyGo(ActivityUtil.getTopActivity(), LoginActivity.class);
                                        ActivityUtil.removeAllActivity();
                                    }
                                }, Constant.TIMER_TIME);


                            } else {
                                ToastUtils.showLong("密码修改失败！");
                            }
                        }
                    }

                    @Override
                    public void onError(Response<String> response) {
                        String result = response.body();
                        LogUtils.e(getClass().getSimpleName(), "onError==" + result);

                    }
                });
    }


    @OnClick(R.id.but_save)
    public void onViewClicked(View v) {
        if (v.getId() == R.id.but_save) {
            oldPassword = MyUtills.getEditText(editOldPassword);
            newPassword = MyUtills.getEditText(editNewPassword);
            sureNewPassword = MyUtills.getEditText(editSureNewPassword);

            if (StringUtils.isEmpty(oldPassword)) {
                ToastUtils.showLong("请输入旧密码！");
                return;
            }
            if (StringUtils.isEmpty(newPassword)) {
                ToastUtils.showLong("请输入新密码！");
                return;
            }

            if (StringUtils.isEmpty(newPassword)) {
                ToastUtils.showLong("请输入确认新密码！");
                return;
            }


            if (!newPassword.equals(sureNewPassword)) {
                ToastUtils.showLong("两次密码输入不一致！");
                return;
            }
            resetPassword();
        }
    }
}